

let iMacrosDownloadDlgHook = (function() {
    "use strict";
    const Cu = Components.utils;
    const Ci = Components.interfaces;
    const Cc = Components.classes;
    function require(url) {
        let scope = {};
        Cu.import(url, scope);
        return scope;
    }
    let {MiscUtils, StrUtils} = require("chrome://jsm/content/utils.js");
    let {Storage} = require("chrome://jsm/content/Storage.js");
    let {Services} = require("resource://gre/modules/Services.jsm");
    let {OS} = require("resource://gre/modules/osfile.jsm");

    return {
        get opener() {
            return this.m_opener ||
                (
                    this.m_opener = MiscUtils.browserWindow
                );
        },

        get playing() {
            return this.opener.iMacros.playing;
        },

        get recording() {
            return this.opener.iMacros.recording;
        },
        
        onAccept() {
            this.opener.iMacros.recorder.handleDownloadDialog();
            dialog.onOK();
        },

        onCancel() {
            
            
            
            this.opener.iMacros.recorder.handleDownloadDialog();
            dialog.onCancel();
        },

        
        hookButtons() {
            let dlg = document.documentElement;
            dlg.setAttribute("ondialogaccept",
                             "return iMacrosDownloadDlgHook.onAccept()");
            dlg.setAttribute("ondialogcancel",
                             "return iMacrosDownloadDlgHook.onCancel()");
        },

        
        showMessage(msg) { 
            let con = document.getElementById("container");
            let hbox = document.createElement("hbox");
            let hbox_label = document.createElement("hbox");
            let hbox_image = document.createElement("hbox");
            let el = document.createElement("label");
            let image = document.createElement("image");
            
            hbox.setAttribute("id", "imacros-commdlg-message");
            hbox.setAttribute("align", "center");
            hbox.appendChild(hbox_label);
            hbox.appendChild(image);
            hbox_label.setAttribute("pack", "center");
            hbox_label.setAttribute("flex", "1");
            hbox_label.appendChild(el);
            
            el.setAttribute("value", msg);
            image.setAttribute("id", "imacros-commdlg-image");
            
            con.insertBefore(hbox, con.firstChild);
            let sss = Cc['@mozilla.org/content/style-sheet-service;1']
                .getService(Ci.nsIStyleSheetService);
            sss.loadAndRegisterSheet(
                Services.io.newURI("chrome://imacros/skin/commdlg.css", 
                                   null, null),
                sss.AUTHOR_SHEET
            );
        },


        init() {
            if (this.recording) {
                this.hookButtons();
                this.showMessage(MiscUtils.strings("imacrosrecordingdialog"));
            } else if (this.playing) {
                this.showMessage(MiscUtils.strings("imacrosreplayingdialog"));
                
                
                
                let mode = document.getElementById("mode");
                let complete = (function() {
                    this.action = Storage.getSpecific(this.opener.iMacros.wid,
                                                      "onDownloadAction");
                    
                    let filename = this.action.filename;
                    let t = null;
                    let loc = document.getElementById("location");
                    if (filename == "*") {
                        filename = loc.value;
                    } else if (t = filename.match(/^\+(.+)/)) {
                        if (/\..*$/.test(loc.value))
                            filename = loc.value.replace(/(.+)(\..*)$/,
                                                         "$1"+t[1]+"$2");
                        else 
                            filename = loc.value+t[1];
                    } else if (!/\.[^\.]+$/.test(filename)) {
                        filename += loc.value.replace(/(?:.+)(\.[^\.]+)$/,"$1");
                    }
                    loc.value = filename;
                    Storage.clearSpecific(this.opener.iMacros.wid,
                                          "onDownloadAction");
                    let folder = this.action.folder, new_loc = "";
                    if (folder == "*") {
                        new_loc = Prefs.defaultDownloadPath;
                    } else {
                        new_loc = OS.Path.split(folder).absolute ? folder :
                            OS.Path.join(Prefs.defaultDownloadPath,
                                         ...OS.Path.split(folder).components);
                    }
                    new_loc = OS.Path.join(new_loc, filename);
                    this.opener.iMacros.player.handleDownloadDialog(
                        dialog.mLauncher.source, new_loc
                    );
                    document.documentElement.cancelDialog();
                }).bind(this)
                
                document.defaultView.addEventListener("activate", complete, false)
             }
        }
    };
})();

iMacrosDownloadDlgHook.init();

